package org.fing.edu.uy.esbadp.init;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.fing.edu.uy.esbadp.admServices.mbean.EsbAdmServices;
import org.fing.edu.uy.esbadp.core.constants.JMXServiceConstants;
import org.fing.edu.uy.esbadp.mbeanServices.EsbAdaptativeCacheServices;
import org.fing.edu.uy.esbadp.modules.admAdaptation.mbean.EsbAdaptativeAdminServices;
import org.fing.edu.uy.esbadp.modules.admRequeriments.mbean.EsbServiceRequirements;
import org.fing.edu.uy.init.properties.mbean.EsbAdaptativePropertiesLoaderServices;
import org.jboss.mx.util.MBeanServerLocator;

public class EsbAdaptativeServicesManager {

	private static java.util.logging.Logger logger = Logger.getLogger(EsbAdaptativeServicesManager.class.getSimpleName());
	
	private static EsbAdaptativeServicesManager instance;
	private static MBeanServer mbeanJBossServer;
	
	private EsbAdaptativeServicesManager(){
		mbeanJBossServer = MBeanServerLocator.locateJBoss();
	}
	
	public static synchronized EsbAdaptativeServicesManager getInstance() {
		if(instance == null)
			instance = new EsbAdaptativeServicesManager();
		return instance;
	}
	
	//Registra todos los MBean
	public void registerAll(){
		try{
			//Registra el MBean EsbAdaptativeCacheServices
			ObjectName name = new ObjectName(JMXServiceConstants.JMXCacheMBean);
			EsbAdaptativeCacheServices mBean = new EsbAdaptativeCacheServices();			
			mbeanJBossServer.registerMBean(mBean, name);

			//Registra el MBean EsbAdaptativeAdminServices 		
			name = new ObjectName(JMXServiceConstants.JMXAdmAdaptationMBean);
			EsbAdaptativeAdminServices mBean3 = new EsbAdaptativeAdminServices();			
			mbeanJBossServer.registerMBean(mBean3, name);
			
			//Registra el MBean EsbRequirementServices		
			name = new ObjectName(JMXServiceConstants.JMXServiceRequerimentMBean);
			EsbServiceRequirements mBean4 = new EsbServiceRequirements();
			mbeanJBossServer.registerMBean(mBean4, name);
			
			//Registra el MBean EsbAdmServices		
			name = new ObjectName(JMXServiceConstants.JMXServicesAdminMBean);
			EsbAdmServices  mBean6 = new EsbAdmServices();
			mbeanJBossServer.registerMBean(mBean6, name);
			
			
			//Registra el MBean para la recarga de las propiedades
			name = new ObjectName(JMXServiceConstants.JMXPropertiesLoader);
			EsbAdaptativePropertiesLoaderServices  mBean7 = new EsbAdaptativePropertiesLoaderServices();
			mbeanJBossServer.registerMBean(mBean7, name);
			
			
		}catch(Exception ex ){
			logger.log(Level.SEVERE, "Exception in registerAll JMX Service.", ex);
		}			 
	}	
	
	//Desregistra todos los MBean EsbAdaptativeCacheServices
	public void unRegisterAll(){
		try{
			ObjectName mServiceBeanName = new ObjectName(JMXServiceConstants.JMXCacheMBean);
			mbeanJBossServer.unregisterMBean(mServiceBeanName);
			
			mServiceBeanName = new ObjectName(JMXServiceConstants.JMXAdmAdaptationMBean);
			mbeanJBossServer.unregisterMBean(mServiceBeanName);
						
			mServiceBeanName = new ObjectName(JMXServiceConstants.JMXServiceRequerimentMBean);
			mbeanJBossServer.unregisterMBean(mServiceBeanName);
			
			mServiceBeanName = new ObjectName(JMXServiceConstants.JMXServicesAdminMBean);
			mbeanJBossServer.unregisterMBean(mServiceBeanName);
		}catch(Exception ex ){
			logger.log(Level.SEVERE, "Exception in unRegisterAll JMX Service.", ex);
		}			 
	}

}
